const blogTextModel = require('../../model/blogTextModel')
const sqlConditions = require('../../utils/sqlCondition')


class blogTextSer {
    // 查询列表
    async getBlogTextList(searchData, listQuery) {
        searchData = sqlConditions(searchData)
        if (searchData && searchData.blog_release_date) {
            searchData.blog_release_date = new Date(searchData.blog_release_date)
        }
        const data = await blogTextModel.findAndCountAll({
            attributes: [
                ['id', 'id'],
                ['blog_title', 'blogTitle'],
                ['blog_type_id_k', 'blogTypeIdK'],
                ['blog_type_name_k', 'blogTypeNameK'],
                ['blog_cover_image', 'blogCoverImage'],
                ['blog_status', 'blogStatus'],
                ['blog_cover_title', 'blogCoverTitle'],
                ['blog_content', 'blogContent'],
                ['blog_page_view', 'blogPageView'],
                ['blog_comment_number', 'blogCommentNumber'],
                ['blog_release_date', 'blogReleaseDate'],
                ['createdAt', 'createdAt'],
                ['updatedAt', 'updatedAt'],
            ],
            where: searchData,
            limit: listQuery.limit,
            offset: listQuery.limit * (listQuery.page - 1),
        })
        return data
    }

    // 查询博客内容
    async getBlogTextOne(searchData) {
        const data = await blogTextModel.findAndCountAll({
            attributes: [
                ['id', 'id'],
                ['blog_title', 'blogTitle'],
                ['blog_type_id_k', 'blogTypeId'],
                ['blog_type_name_k', 'blogTypeName'],
                ['blog_cover_image', 'blogCoverImage'],
                ['blog_status', 'blogStatus'],
                ['blog_cover_title', 'blogCoverTitle'],
                ['blog_content', 'blogContent'],
                ['blog_page_view', 'blogPageView'],
                ['blog_comment_number', 'blogCommentNumber'],
                ['createdAt', 'createdAt'],
                ['updatedAt', 'updatedAt'],
            ],
            where: searchData,
        })
        return data
    }

    //查询博客浏览次数和评论次数
    async searchViewAndComment(id) {
        const data = await blogTextModel.findAll({
            attributes: [
                ['blog_page_view', 'blogPageView'],
                ['blog_comment_number', 'blogCommentNumber'],
            ],
            where: {
                id: id
            },
        })
        return data
    }

    // 添加
    async addBlogText(reqData) {
        reqData = sqlConditions(reqData)
        const addReturnData = await blogTextModel.create(reqData)
        return addReturnData
    }

    // 删除
    async deleteBlog(reqData) {
        const data = await blogTextModel.destroy({
            where: reqData
        })

        return data
    }

    // 修改
    async editBlog(where, reqData) {
        reqData = sqlConditions(reqData)
        const data = await blogTextModel.update(reqData, {where: where})
        return data
    }

}

module.exports = new blogTextSer()
